package org.gcu.filestorage.controller;

import lombok.extern.slf4j.Slf4j;
import org.gcu.filestorage.api.ErrorCode;
import org.gcu.filestorage.pojo.ApplyRecord;
import org.gcu.filestorage.pojo.RequestResult;
import org.gcu.filestorage.serive.ApplyInfoService;
import org.gcu.filestorage.uitls.TokenUtil;
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
public class ApplyController {

    private final ApplyInfoService applyInfoService;

    @Autowired
    public ApplyController(ApplyInfoService applyInfoService) {
        this.applyInfoService = applyInfoService;
    }


    @PostMapping("/submitApply")
    public RequestResult<Boolean> submitApply(@RequestHeader("Authorization") String token, @RequestBody Map<String, String> requestParams) {
        log.info("submitApply(), requestParams=" + requestParams);
        RequestResult<Boolean> requestResult = new RequestResult<>();
        String account = TokenUtil.parseAccount(token);
        int applyType = Integer.parseInt(requestParams.getOrDefault("applyType", "1"));
        int applyStatus = 1;
        long applyVolume = Long.parseLong(requestParams.getOrDefault("applyVolume", "0"));
        String applyReason = requestParams.getOrDefault("applyReason", "");
        long applyTime = new Date().getTime();
        boolean success = this.applyInfoService.insertNewApplyRecord(account, applyType, applyStatus, applyVolume, applyReason, applyTime);
        if (success) {
            requestResult.setData(true);
            requestResult.setMessage("申请成功");
            requestResult.setResCode(ErrorCode.SUCCESS);
        } else {
            requestResult.setData(false);
            requestResult.setMessage("申请失败");
            requestResult.setResCode(ErrorCode.SUBMIT_APPLY_FAILED);
        }
        return requestResult;
    }

    @PostMapping("/queryApplyRecord")
    public RequestResult<String> queryApplyRecord(@RequestHeader("Authorization") String token) throws JSONException {
        log.info("queryApplyRecord(), token=" + token);
        RequestResult<String> requestResult = new RequestResult<>();
        String account = TokenUtil.parseAccount(token);
        List<ApplyRecord> applyRecordList = applyInfoService.queryApplyRecord(account);
        requestResult.setResCode(ErrorCode.SUCCESS);
        JSONArray jsonArray = getApplyRecordJsonArray(applyRecordList);
        requestResult.setData(jsonArray.toString());
        requestResult.setMessage("查询成功");
        return requestResult;
    }

    @NotNull
    private static JSONArray getApplyRecordJsonArray(List<ApplyRecord> applyRecordList) throws JSONException {
        JSONArray jsonArray = new JSONArray();
        for(ApplyRecord applyRecord : applyRecordList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("applyTime", applyRecord.getApplyTime());
            jsonObject.put("applyType", applyRecord.getApplyType());
            jsonObject.put("applyReason", applyRecord.getApplyReason());
            jsonObject.put("applyStatus", applyRecord.getApplyStatus());
            jsonObject.put("applyVolume", applyRecord.getApplyVolume());
            jsonObject.put("approvalDesc", applyRecord.getApprovalDesc());
            jsonArray.put(jsonObject);
        }
        return jsonArray;
    }
}
